package com.vgemv.jsutilitysdk.Orientation;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.WindowManager;

/**
 * 使用时需要先init初始化context.
 */

public class JsOrientationManager {

    public int mSensorOrientation = JsSensorOrientationUnkown;
    public final static int JsSensorOrientationUnkown = -1;
    public final static int JsSensorOrientationPortraitUp = 0;
    public final static int JsSensorOrientationLandscapeRight = 1;
    public final static int JsSensorOrientationProtraitDown = 2;
    public final static int JsSensorOrientationLandscapeLeft = 3;


    public final static int JsScreenOrientationPortraitUp = 0;
    public final static int JsScreenOrientationLandscapeRight = 1;
    public final static int JsScreenOrientationProtraitDown = 2;
    public final static int JsScreenOrientationLandscapeLeft = 3;

    public final static String JsOrientationBroadcastSensorOrientationDidChange = "JsOrientationBroadcastSensorOrientationDidChange";
    public final static String JsOrientationOld = "JsOrientationOld";
    public final static String JsOrientationNew = "JsOrientationNew";


    public JsOrientationManagerCallback mCallback;
    private Application mContext;

    private static JsOrientationManager _sharedInstance;

    private OrientationEventListener mOrientationListener;

    JsOrientationManager() {

    }

    public static void initClass(Application application) {
        if(_sharedInstance == null) {
            _sharedInstance = new JsOrientationManager();
            _sharedInstance.mContext = application;
            _sharedInstance.setupOrientationListener();
        }
    }

    public void startMonitoring() {
        if(mOrientationListener == null) {
            // 提示需要先init.
        }
        else {
            mOrientationListener.enable();
        }

    }

    public static JsOrientationManager sharedInstance() {
        return _sharedInstance;
    }

    private void setupOrientationListener() {
        mOrientationListener = new OrientationEventListener(mContext) {
            @Override
            public void onOrientationChanged(int orientation) {
//                String message = String.format("Current orientation is %d",orientation);
//                Log.d("Orientation",message);
                int currentSensorOrientation = JsSensorOrientationUnkown;
                if (orientation >= 315 || orientation < 45) {
                    currentSensorOrientation = JsSensorOrientationPortraitUp;
                } else if (orientation >= 45 && orientation < 135) {
                    currentSensorOrientation = JsSensorOrientationLandscapeRight;
                } else if (orientation >= 135 && orientation < 225) {
                    currentSensorOrientation = JsSensorOrientationProtraitDown;
                } else {
                    currentSensorOrientation = JsSensorOrientationLandscapeLeft;
                }

                if (mSensorOrientation != currentSensorOrientation) {
                    String msg = String.format("Old orientation is %d,new orientation is %d",currentSensorOrientation,mSensorOrientation);
                    Log.d("JsOrientationManger",msg);
                    emitEvent(currentSensorOrientation, mSensorOrientation);
                }
            }
        };
    }

    private void emitEvent(int newOrientation, int oldOrientation) {
        mSensorOrientation = newOrientation;
        //callback
//                if(mCallback != null) {
//                    mCallback.jsOrientationManagerBeforeSensorOrientationChange(newOrientation,oldOrientation);
//                }
        //发送广播
        Intent intent = new Intent();
        intent.setAction(JsOrientationBroadcastSensorOrientationDidChange);
        intent.putExtra(JsOrientationOld,oldOrientation);
        intent.putExtra(JsOrientationNew,newOrientation);
        mContext.sendBroadcast(intent);
    }


    //能真实反映当前的屏幕方向.
    //只有四个值.
    public static int getScreenOrientation(final Context context) {
        final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        switch (wm.getDefaultDisplay().getRotation()) {
            case Surface.ROTATION_90: {
                return JsScreenOrientationLandscapeLeft;
            }
            case Surface.ROTATION_180: {
                return JsScreenOrientationProtraitDown;
            }
            case Surface.ROTATION_270: {
                return JsScreenOrientationLandscapeRight;
            }
            default: {
                return JsScreenOrientationPortraitUp;
            }
        }
    }

}
